package com.extension.company.view.main;


import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.extension.company.Constant;
import com.extension.company.admin.R;
import com.extension.company.model.entity.ProvinceCityBean;
import com.extension.company.common.http.RetrofitHelper;
import com.extension.company.common.mvc.BaseActivity;
import com.extension.company.common.utils.ToastUtil;
import com.extension.company.model.api.RetrofitUrlApi;
import com.umeng.socialize.UMShareAPI;

import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * 主activity
 */
public class MainActivity extends BaseActivity implements View.OnClickListener {

	@BindView(R.id.ll_home)
	LinearLayout ll_home;
	@BindView(R.id.ll_samecity)
	LinearLayout ll_makemoney;
	@BindView(R.id.ll_income)
	LinearLayout ll_income;
	@BindView(R.id.ll_car)
	LinearLayout ll_car;
	@BindView(R.id.ll_my)
	LinearLayout ll_my;
	@BindView(R.id.iv_home)
	ImageView iv_home;
	@BindView(R.id.img_samecity)
	ImageView img_samecity;
	@BindView(R.id.iv_income)
	ImageView iv_income;
	@BindView(R.id.iv_car)
	ImageView iv_car;
	@BindView(R.id.iv_my)
	ImageView iv_my;
	@BindView(R.id.tv_home)
	TextView tv_home;
	@BindView(R.id.tv_simecity)
	TextView tv_samecity;
	@BindView(R.id.tv_income)
	TextView tv_income;
	@BindView(R.id.tv_car)
	TextView tv_car;
	@BindView(R.id.tv_my)
	TextView tv_my;


	private FragmentHome fragmentHome;   // 首页
	//	private FragmentMakeMoney fragmentMakeMoney; // 去赚钱
	private FragmentSameCity fragmentSameCity;  // 同城
	private FragmentIncome fragmentIncome;   // 收益
	private FragmentCar fragmentCar;   //购物车
	private FragmentMy fragmentMy;     // 我的
	private FragmentManager fragmentManager;
	private Fragment mContent;

	private long mExitTime;

	@Override
	protected void initView() {
		fragmentHome = new FragmentHome();
//		fragmentMakeMoney = new FragmentMakeMoney();
		fragmentSameCity = new FragmentSameCity();
		fragmentIncome = new FragmentIncome();
		fragmentCar = new FragmentCar();
		fragmentMy = new FragmentMy();
		fragmentManager = getSupportFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.fm_View, fragmentHome).commit();
		//记录当前页面
		mContent = fragmentHome;
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
					| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
		}
	}

	@Override
	protected void initData() {
		getProvinceCity();

	}

	@Override
	protected void initListener() {
		ll_home.setOnClickListener(this);
		ll_makemoney.setOnClickListener(this);
		ll_income.setOnClickListener(this);
		ll_car.setOnClickListener(this);
		ll_my.setOnClickListener(this);
	}

	@Override
	public int getMainLayout() {
		return R.layout.activity_main;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.ll_home:  // 首页
				setStatus(0);
				break;
			case R.id.ll_samecity:  // 去赚钱
				setStatus(1);
				break;
			case R.id.ll_income:   // 收益
				setStatus(2);
				break;
			case R.id.ll_car:     // 购物车
				setStatus(3);
				break;
			case R.id.ll_my:     // 我的
				setStatus(4);
				break;
			default:
				break;
		}
	}

	/**
	 * 功能：更改底部菜单的选中状态
	 *
	 * @param temp 当前选中的菜单
	 */
	private void setStatus(int temp) {

		iv_home.setImageResource(R.drawable.shouyehui);       // 将所有的底部菜单的图标都设置未选中状态
		img_samecity.setImageResource(R.drawable.samecity);
		iv_income.setImageResource(R.drawable.shouyi);
		iv_car.setImageResource(R.drawable.gouwuh);
		iv_my.setImageResource(R.drawable.wode);

		tv_home.setTextColor(Color.parseColor("#A0A0A0"));  // 将所有的底部菜单的说明都设置成未选中状态
		tv_samecity.setTextColor(Color.parseColor("#A0A0A0"));
		tv_income.setTextColor(Color.parseColor("#A0A0A0"));
		tv_car.setTextColor(Color.parseColor("#A0A0A0"));
		tv_my.setTextColor(Color.parseColor("#A0A0A0"));

		if (temp == 0) {                                         // 根据选中的，更改底部菜单和字体的颜色
			iv_home.setImageResource(R.drawable.shouyehuan);
			tv_home.setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, fragmentHome);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			}
		} else if (temp == 1) {
			img_samecity.setImageResource(R.drawable.simlecityselected);
			tv_samecity.setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, fragmentSameCity);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
				this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
			}
		} else if (temp == 2) {
			iv_income.setImageResource(R.drawable.shouyihuang);
			tv_income.setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, fragmentIncome);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
				this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			}
		} else if (temp == 3) {
			iv_car.setImageResource(R.drawable.gouwucxz);
			tv_car.setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, fragmentCar);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
				this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
			}
		} else if (temp == 4) {
			iv_my.setImageResource(R.drawable.wodehuang);
			tv_my.setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, fragmentMy);
			if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
				this.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
						| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
			}
		}
	}


	/**
	 * 切换fragment 界面
	 *
	 * @param from 当前的frgment
	 * @param to   选中的fragment
	 */
	public void switchContent(Fragment from, Fragment to) {

		if (mContent != to) {
			mContent = to;  //记住当前的fragment
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			// 先判断是否被add过
			if (!to.isAdded()) {
				// 隐藏当前的fragment，add下一个到Activity中
				transaction.hide(from).add(R.id.fm_View, to).commit();
			} else {
				// 隐藏当前的fragment，显示下一个
				transaction.hide(from).show(to).commit();
			}
		}
	}


	@Override
	public void onBtnCancel() {
		if ((System.currentTimeMillis() - mExitTime) > 2000) {
			ToastUtil.showToast(this, "再按一次退出应用", Toast.LENGTH_SHORT);
			mExitTime = System.currentTimeMillis();
		} else {
			finish();
		}
	}

	// zwk，UM
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
	}


	/**
	 * 获取省市区的json
	 */
	private void getProvinceCity() {
		RetrofitUrlApi retrofitUrlApi = RetrofitHelper.getRetrofit().create(RetrofitUrlApi.class);
		retrofitUrlApi.ProvinceCity(Constant.SIGN, Constant.TOKEN)
				.observeOn(Schedulers.newThread())
				.doOnNext(new Consumer<ProvinceCityBean>() {
					@Override
					public void accept(ProvinceCityBean provinceCityBean) throws Exception {
						Log.d("zwk", "子线程中进行数据库的操作" + provinceCityBean.getCode());
						if (200 == provinceCityBean.getCode()) {

						}

					}
				})
				.subscribeOn(Schedulers.io())
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<ProvinceCityBean>() {
					@Override
					public void onSubscribe(Disposable d) {

					}

					@Override
					public void onNext(ProvinceCityBean provinceCityBean) {
						if (200 == provinceCityBean.getCode()) {
							Log.d("zwk", "获取到了信息了");
						}

					}

					@Override
					public void onError(Throwable e) {

					}

					@Override
					public void onComplete() {

					}
				});
	}
}
